package com.tgy.admin.Service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.tgy.admin.Service.CategoryService;
import com.tgy.admin.util.BeanFieldUtils;
import com.tgy.core.service.BlogArticleService;
import com.tgy.core.service.BlogCategoryService;
import com.tgy.page.PageQuery;
import com.tgy.pojo.BlogArticle;
import com.tgy.pojo.BlogCategory;
import com.tgy.result.GraceJSONResult;
import com.tgy.result.LayuiTableResult;
import com.tgy.vo.BlogCategoryVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author tianguiyin
 * @version 1.0
 * @date 2022/6/21 21:04
 *
 * 后台用户业务实现
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private BlogCategoryService blogCategoryService;
    @Resource
    private BlogArticleService blogArticleService;
    @Resource
    private BeanFieldUtils beanFieldUtils;


    @Override
    public LayuiTableResult getBlogCategoryList(BlogCategory bo, PageQuery pageQuery) {
        return blogCategoryService.queryLayuiPageList(bo,pageQuery);
    }


    @Override
    public GraceJSONResult addBlogCategory(BlogCategory bo) {
        try {
            beanFieldUtils.add(bo);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return GraceJSONResult.ok(blogCategoryService.insertByBo(bo));
    }

    @Override
    public GraceJSONResult updateBlogCategory(BlogCategory bo) {
        try {
            beanFieldUtils.update(bo);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return GraceJSONResult.ok(blogCategoryService.updateByBo(bo));
    }

    @Override
    public GraceJSONResult delBlogCategory(String id) {
        return GraceJSONResult.ok(blogCategoryService.deleteById(id));
    }

    @Override
    public GraceJSONResult delBlogCategoryIds(List<String> ids) {
        return GraceJSONResult.ok(blogCategoryService.deleteWithValidByIds(ids,false));
    }

    @Override
    public GraceJSONResult getBlogCategoryById(String id) {
        return GraceJSONResult.ok(blogCategoryService.queryById(id));
    }

    @Override
    public LayuiTableResult getAllSysRoleList(String userId, PageQuery pageQuery) {
        return null;
    }

    @Override
    public List<BlogCategoryVo> getCategoryList(String articleId) {
        List<BlogCategoryVo> list = new ArrayList<>();
        BlogCategory bo = new BlogCategory();
        List<BlogCategory> blogCategories = blogCategoryService.queryList(bo);

        String categoryId = "";
        if (StrUtil.isNotBlank(articleId)){
            // 查询文章中的信息
            BlogArticle blogArticle = blogArticleService.queryById(articleId);
            categoryId = blogArticle.getCategoryId();
        }

        List<String> categoryIds = new ArrayList<>(Arrays.asList(categoryId.split(",")));
        for (BlogCategory blogCategory : blogCategories) {
            BlogCategoryVo vo = new BlogCategoryVo();
            BeanUtil.copyProperties(blogCategory,vo);
            if (categoryIds.contains(blogCategory.getId())){
                vo.setSelected(true);
            }
            list.add(vo);
        }
        return list;
    }
}
